package com.briup.jz.service.impl;


import com.briup.jz.bean.Comment;
import com.briup.jz.bean.CommentExample;
import com.briup.jz.bean.extend.CommentExtend;
import com.briup.jz.dao.CommentMapper;
import com.briup.jz.dao.OrderMapper;
import com.briup.jz.dao.extend.CommentExtendMapper;
import com.briup.jz.service.ICommentService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @project: jz
 * @description:
 * @create: 2020/6/13 23:07
 */
@Service
public class CommentServiceimpl implements ICommentService {
    @Resource
    private CommentMapper commentMapper;
    @Resource
    private CommentExtendMapper commentExtendMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 1. 发表/修改评论
     */
    //检查订单状态是否为“待评论”
    @Override
    public void checkOrderStatus(long id) {
        //1. 先查询该订单状态
        String status = orderMapper.selectByPrimaryKey(id).getStatus();
        //2. 比较
        if(status.equals("待评论")){
            //“待评论”，可以评论
            return;
        }
        //不能评论
        throw new CustomerException("不能评论，因为此订单的状态不为“待评论”");
    }

    //发表/修改评论
    @Override
    public void commentOrUpdate(Comment comment) {
        comment.setCommentTime(new Date().getTime());
        if(comment.getId() != null){
            //修改
            commentMapper.updateByPrimaryKey(comment);
        } else {
            //发表
            comment.setStatus(CommentExtend.NOTAPPROVED);
            commentMapper.insert(comment);

        }

    }

    /**
     *  2.查看评论
     */
    //查找评论（无级联）
    @Override
    public Comment findById(long id) {
        return commentMapper.selectByPrimaryKey(id);
    }

    //查找评论和子评论（级联）
    @Override
    public CommentExtend findByIdWithFollow(long id) {
        return commentExtendMapper.selectByIdWithFollow(id);
    }

    //浏览所有评论（无级联）
    @Override
    public List<Comment> findAllComments() {
        return commentExtendMapper.selectAllComments();
    }

    //浏览所有评论（级联）
    @Override
    public List<CommentExtend> findAllWithFollow() {
        return commentExtendMapper.selectAllWithFollow();
    }

    /**
     * 3. 删除评论
     */
    //删除评论
    @Override
    public void deleteById(long id) {
        commentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 4. 审核评论
     */
    //修改评论的状态
    @Override
    public void updateStatus(long id, String stauts) {
//        String status_result = stauts.equals("1")? CommentExtend.APPROVED: CommentExtend.FAILED;
        commentExtendMapper.updateStatus(id, stauts);
    }


    /**
     * 5. 顾客投诉
     */
    //顾客发起投诉
    @Override
    public void initiateComplaint(long orderId, long userId, String content) {
        Comment comment = new Comment();
        comment.setScore(-1);
        comment.setStatus(CommentExtend.NOTARBITRATED);
        comment.setCommentTime(new Date().getTime());
        comment.setOrderId(orderId);
        comment.setUserId(userId);
        comment.setContent(content);
        commentMapper.insert(comment);
    }
}
